from flask_login import logout_user, current_user
from forms.login_form import LoginForm
from models.vep import Vep,VepTest
from routes import app
from flask import render_template, abort, redirect, url_for, flash
from services.user_service import UserService
from flask import Flask, request, jsonify

@app.route('/vep', methods=['GET'])
def vep_data():
    region = request.args.get('vepRegion', '商州区')
    year = request.args.get('vepYear', 2023, type=int)

    # 查询数据库
    result = Vep.query.filter_by(region=region, year=year).first()
    print('year',year)
    print('result',result)

    if result:
        data = {
            'vepRegion':result.region,
            'vepYear': result.year,
            'regionArea': result.region_area,
            'regionRegulationService': result.region_regulation_service,
            'regionWaterPurificationGep': result.region_water_purification_gep,
            'avgSoilCarbonStorage': result.avg_soil_carbon_storage,
            'avgVegetationCarbonStorage': result.avg_vegetation_carbon_storage
        }
        return jsonify(data)
    else:
        return jsonify({'error': 'Data not found'}), 404

@app.route('/vepTest', methods=['GET'])
def vep_test():
    vepName = request.args.get('vepName', '仓颉小镇')
    # 查询数据库
    result = VepTest.query.filter_by(vepName = vepName).first()
    print('vepName',result)
    if result:
        data = {
            'vepName':result.vepName,
            'inAnnualProductValue': result.inAnnualProductValue,
            'inAnnualTotalCost': result.inAnnualTotalCost,
            'inProcessProfit': result.inProcessProfit,
            'inCleanEnergyGeneration': result.inCleanEnergyGeneration,
            'inElectricityPrice': result.inElectricityPrice,
            'inTourismRevenue': result.inTourismRevenue,
            'inHotelRoomCount': result.inHotelRoomCount,
            'inHotelPrice': result.inHotelPrice,
            'inAnnualOccupancy': result.inAnnualOccupancy,
            'inPropertyProfit': result.inPropertyProfit,
            'inDailyTraffic': result.inDailyTraffic,
            'inTourismHealthProfit': result.inTourismHealthProfit,
            'inProjectLandArea': result.inProjectLandArea,

            'inSoilCarbonStorage': result.inSoilCarbonStorage,
            'inVegetationCarbonStorage': result.inVegetationCarbonStorage,
            'inTotalNitrogen': result.inTotalNitrogen,
            'inTotalPhosphorus': result.inTotalPhosphorus,
            'inAmmoniaNitrogen': result.inAmmoniaNitrogen,
            'inBod5': result.inBod5,
            'inCodCr': result.inCodCr,
            'vepTestValue': result.vepTestValue
        }
        return jsonify(data)
    else:
        return jsonify({'error': 'Data not found'}), 404